package com.windoent.yugioh.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.windoent.yugioh.R;
import com.windoent.yugioh.utils.BaseMethod;
import com.windoent.yugioh.utils.BaseUtils;

/**
 * author : robert
 * date   : 2021/4/28 11:23
 * desc   :
 */
public class MessageItem extends RelativeLayout {
    private Context mConText;
    private RelativeLayout mRlMsg;
    private TextView mTvMsgNumbers;
    private String tvhint;
    private LinearLayout mLlMsg;


    public MessageItem(Context context) {
        this(context, null, 0);
    }

    public MessageItem(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MessageItem(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mConText = context;
        LayoutInflater.from(mConText).inflate(R.layout.custom_view_message_item, this);
        TypedArray attributes = mConText.obtainStyledAttributes(attrs, R.styleable.UserInfoItem, 0, 0);
        try {
            tvhint = attributes.getString(R.styleable.UserInfoItem_tvhint);
        } finally {
            attributes.recycle();
        }
        setUpView();
    }

    private void setUpView() {
        mRlMsg = (RelativeLayout) findViewById(R.id.rl_msg);
        mTvMsgNumbers = (TextView) findViewById(R.id.tv_msg_numbers);
        mTvMsgNumbers.setText(tvhint);
        mLlMsg = (LinearLayout) findViewById(R.id.ll_msg);
        if (BaseMethod.isNull(tvhint) || tvhint.equals("0")) {
            mLlMsg.setVisibility(INVISIBLE);
        }
    }

    public void setTvTitle(String s) {
        if (BaseMethod.isNull(s) || s.equals("0")) {
            mLlMsg.setVisibility(INVISIBLE);
        } else if (BaseMethod.notNull(s) && BaseUtils.stringToInt(s) > 99) {
            mLlMsg.setVisibility(VISIBLE);
            mTvMsgNumbers.setText("99");
        } else {
            mLlMsg.setVisibility(VISIBLE);
            mTvMsgNumbers.setText(s);
        }
    }

    public void setListener(OnClickListener listener) {
        mRlMsg.setOnClickListener(listener);
    }
}
